package com.csk.zsk.compents.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.csk.zsk.commons.dao.impl.BaseHibernateDao;
import com.csk.zsk.compents.dao.MusicDao;
import com.csk.zsk.compents.entity.Music;

@Repository
public class MusicDaoImpl extends BaseHibernateDao<Music, String> implements MusicDao {

	private static final String QUERY_NEW_MUSIC = "from Music m";
	private static final String QUERY_MUSIC_BY_LIKE = "from  Music v where v.songName like ? or v.singerName like ?";
	private static final String QUERY_MUSIC_BY_IDLIST = "from  Music v where v.id in (:ids)";

	@SuppressWarnings("unchecked")
	@Override
	public List<Music> getNewMusic(Integer num) {
		return createQuery(QUERY_NEW_MUSIC).setMaxResults(num).list();
	}

	@Override
	public List<Music> searchMusicByLike(String musicLike) {
		return find(QUERY_MUSIC_BY_LIKE, "%" + musicLike + "%", "%" + musicLike + "%");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Music> getMusicListByIds(List<String> idList) {
		Query query = getSession().createQuery(QUERY_MUSIC_BY_IDLIST);
		if (idList != null && idList.size() > 0) {
			return query.setParameterList("ids", idList).list();
		} else {
			return null;
		}
	}

}
